package com.hmyd.hmyd_android.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hmyd.hmyd_android.AppBasic;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.dto.JfMallItemDto;
import com.hmyd.hmyd_android.utils.Utils;
import com.hmyd.hmyd_android.view.InclineTextView;

/**
 * 积分商城商品适配器
 * 
 * @author wangk
 */
public class JfMallItemAdapter extends BaseAdapter {

	private Context context;
	private List<JfMallItemDto> items;
	Intent intent = new Intent();
	private int globalMarginLeft = 0;
	private int globalMarginTop = 0;

	public JfMallItemAdapter(Context context, List<JfMallItemDto> items) {
		this.context = context;
		this.items = items;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position % getCount());
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void refreshData(List<JfMallItemDto> items) {
		this.items.clear();
		this.items.addAll(items);
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewholder;
		if (convertView == null) {
			viewholder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.view_jfmall_item, null);
			viewholder.itemCollectBtn = (ImageView) convertView
					.findViewById(R.id.item_collect);
			viewholder.itemTitle = (TextView) convertView
					.findViewById(R.id.item_title);
			viewholder.itemIcon = (ImageView) convertView
					.findViewById(R.id.image1);
			viewholder.itemPrice = (TextView) convertView
					.findViewById(R.id.item_price);
			viewholder.orgPrice = (TextView) convertView
					.findViewById(R.id.old_price);
			viewholder.orgPriceHua = (RelativeLayout) convertView
					.findViewById(R.id.orgPriceHua);
			viewholder.discount_label = (InclineTextView) convertView.
					findViewById(R.id.discount_label);
		
			convertView.setTag(viewholder);
		} else {
			viewholder = (ViewHolder) convertView.getTag();
		}
		final JfMallItemDto dto = items.get(position);
		viewholder.itemTitle.setText(dto.title);
		viewholder.itemPrice.setText(dto.presentPrice + "");
		String iconTag = dto.icon.replace("[^\\w]", "").trim();
		viewholder.itemIcon.setTag(iconTag);
		viewholder.itemIcon.setImageResource(R.drawable.view_no_photo);
		
		ViewGroup.MarginLayoutParams params = (MarginLayoutParams) viewholder.discount_label.getLayoutParams();
		// 定义全局marginleft，取最大(负数最大)
		if(-viewholder.discount_label.getMeasuredWidth()*2/3 != 0) {
			if(globalMarginLeft == 0) {
				globalMarginLeft =  -viewholder.discount_label.getMeasuredWidth()*2/3;
			}
			if(globalMarginLeft < -viewholder.discount_label.getMeasuredWidth()*2/3) {
				globalMarginLeft =  -viewholder.discount_label.getMeasuredWidth()*2/3;
			}
		}
		// 定义全局margintop,取最小
		if(viewholder.discount_label.getMeasuredHeight() != 0) {
			if(globalMarginTop == 0) {
				globalMarginTop = (int) (viewholder.discount_label.getMeasuredHeight()*0.7);
			}
			if(globalMarginTop > viewholder.discount_label.getMeasuredHeight()*0.7) {
				globalMarginTop = (int) (viewholder.discount_label.getMeasuredHeight()*0.7);
			}
		}
		params.leftMargin = globalMarginLeft;
		params.topMargin = -globalMarginTop;
		viewholder.discount_label.setLayoutParams(params);
		// 防止图片错乱、重复、闪烁
		if (viewholder.itemIcon.getTag() != null
				&& viewholder.itemIcon.getTag().equals(iconTag)) {
			AppBasic.getBitmapUtils(context).display(viewholder.itemIcon,
					dto.icon);
			if(!TextUtils.isEmpty(dto.label)) {
				viewholder.discount_label.setVisibility(View.VISIBLE);
				viewholder.discount_label.setText(dto.label);
			} else {
				viewholder.discount_label.setVisibility(View.INVISIBLE);
			}
		}
		viewholder.itemCollectBtn.setTag(iconTag);
	
		if (viewholder.itemCollectBtn.getTag() != null
				&& viewholder.itemCollectBtn.getTag().equals(iconTag)) {
			if (dto.isCollection) {
				viewholder.itemCollectBtn
						.setImageResource(R.drawable.ji_icon_soud);
			} else {
				viewholder.itemCollectBtn
						.setImageResource(R.drawable.ji_icon_souu);
			}
		}

		if (dto.originalPrice > 0 && dto.originalPrice != dto.presentPrice) {
			viewholder.orgPriceHua.setVisibility(View.VISIBLE);
			viewholder.orgPrice.setText(String.valueOf(dto.originalPrice));
			
			float disCountLabel = (dto.presentPrice/(float)dto.originalPrice) * 10f;
			DecimalFormat df = new DecimalFormat("0.0");
			String tempLabel = df.format(disCountLabel);
			if(tempLabel.endsWith("0")) {
				tempLabel = tempLabel.substring(0, tempLabel.lastIndexOf("."));
			}
			viewholder.discount_label.setVisibility(View.VISIBLE);
			viewholder.discount_label.setText(tempLabel+"折");
		} else {
			viewholder.orgPriceHua.setVisibility(View.GONE);
			viewholder.discount_label.setVisibility(View.INVISIBLE);
		}
		
		return convertView;
	}

	class ViewHolder {
		ImageView itemIcon;
		TextView itemTitle;
		TextView itemPrice;
		ImageView itemCollectBtn;
		TextView orgPrice;
		RelativeLayout orgPriceHua;
		InclineTextView discount_label;
	}

}
